package com.intelligent.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligent.pojo.ProductionWarehousing;
import com.intelligent.service.ProductionWarehousingService;
import com.intelligent.mapper.ProductionWarehousingMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
* @author lenovo
* @description 针对表【production_warehousing(生产入库表)】的数据库操作Service实现
* @createDate 2025-09-08 10:02:48
*/
@Service
public class ProductionWarehousingServiceImpl extends ServiceImpl<ProductionWarehousingMapper, ProductionWarehousing>
    implements ProductionWarehousingService{

    @Override
    public IPage<ProductionWarehousing> getProductionWarehousingPage(
            Page<ProductionWarehousing> page,
            String warehousingNo,
            Integer warehousingStatus,
            String workOrderNo,
            Long warehouseId,
            Integer uId,
            Date startTime,
            Date endTime) {
        
        return this.baseMapper.selectProductionWarehousingPage(
                page, warehousingNo, warehousingStatus, workOrderNo, 
                warehouseId, uId, startTime, endTime
        );
    }

    @Override
    public ProductionWarehousing getProductionWarehousingById(Long id) {
        return this.baseMapper.selectProductionWarehousingById(id);
    }

    @Override
    public boolean addProductionWarehousing(ProductionWarehousing productionWarehousing) {
        try {
            // 自动生成入库单号
            String warehousingNo = generateWarehousingNo();
            productionWarehousing.setWarehousingNo(warehousingNo);
            
            // 设置入库时间为当前时间
            if (productionWarehousing.getWarehousingTime() == null) {
                productionWarehousing.setWarehousingTime(new Date());
            }
            
            // 执行插入操作
            int result = this.baseMapper.insertProductionWarehousing(productionWarehousing);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("新增生产入库记录失败：" + e.getMessage(), e);
        }
    }

    @Override
    public boolean updateProductionWarehousing(ProductionWarehousing productionWarehousing) {
        try {
            // 执行更新操作
            int result = this.baseMapper.updateProductionWarehousing(productionWarehousing);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("修改生产入库记录失败：" + e.getMessage(), e);
        }
    }

    @Override
    public boolean deleteProductionWarehousing(Long id) {
        try {
            // 执行删除操作
            int result = this.baseMapper.deleteProductionWarehousing(id);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("删除生产入库记录失败：" + e.getMessage(), e);
        }
    }

    /**
     * 生成入库单号
     * 格式：RK + yyyyMMdd + 4位递增序号
     * 
     * @return 入库单号
     */
    private String generateWarehousingNo() {
        // 获取当前日期
        String dateStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        
        // 查询当天最大的入库单号
        String maxWarehousingNo = this.baseMapper.selectMaxWarehousingNoByDate(dateStr);
        
        int sequence = 1;
        if (maxWarehousingNo != null && maxWarehousingNo.length() >= 12) {
            // 提取序号部分并递增
            try {
                String sequenceStr = maxWarehousingNo.substring(10); // RK + 8位日期 = 10位
                sequence = Integer.parseInt(sequenceStr) + 1;
            } catch (NumberFormatException e) {
                sequence = 1;
            }
        }
        
        // 格式化序号为4位数字
        return "RK" + dateStr + String.format("%04d", sequence);
    }
}




